//
//  NetRequst.m
//  CC_Weather
//
//  Created by rimi on 16/9/23.
//  Copyright © 2016年 cc. All rights reserved.
//

#import "NetRequst.h"

@implementation NetRequst

+(void)GET:(NSString *)url parameters:(NSString *)parameters success:(void (^)(id))successful fail:(void (^)(NSError *))fail{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //请求超时时间
    manager.requestSerializer.timeoutInterval = 5;
    //请求格式
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //返回格式
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //扩展多种返回类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
    
    [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successful(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
    
}

+(void)POST:(NSString *)url parameters:(NSString *)parameters success:(void (^)(id))successful fail:(void (^)(NSError *))fail{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //请求超时时间
    manager.requestSerializer.timeoutInterval = 8;
    //请求格式
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //返回格式
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //扩展多种返回类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
    
    [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successful(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
    
}


+ (void)cheackTheNet:(void (^)(BOOL))isnet{
    //开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    // 监听网络状态回调
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        // 开启网络状态监听后，只要网络状态发生改变就会调用该 Block 代码段
        // 获取网络连接状态
        //NSString *netStatusStr = [[AFNetworkReachabilityManager sharedManager] localizedNetworkReachabilityStatusString];
        //NSLog(@"%@",netStatusStr);
        
        // 判断网络是否连接
        BOOL isReachable = [AFNetworkReachabilityManager sharedManager].isReachable;
        
        // 判断 WiFi 是否连接
        BOOL isReachableViaWiFi = [AFNetworkReachabilityManager sharedManager].isReachableViaWiFi;
        
        // 判断 无线网络 是否连接
        BOOL isReachableViaWWAN = [AFNetworkReachabilityManager sharedManager].isReachableViaWWAN;
        
        if (isReachable || isReachableViaWWAN || isReachableViaWiFi) {
            isnet(YES);
        }else{
            isnet(NO);
        }
    }];
}


+ (void)alert:(NSString *)string{
    //弹出警告框：没有网络
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:string message:nil preferredStyle:UIAlertControllerStyleAlert];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [alert dismissViewControllerAnimated:YES completion:nil];
        
    });
}
@end
